package crudfx.data;
import java.sql.*;
/**
 * @profile desktop
 * @author Sergey Surikov
 */
public class JDBCConnection {
    public var url:String;
    public var driver:String;
    public var cn:Connection=null;
    public function execute(sql:String,limit:Integer):Fields[]{
        if(cn==null)cn=JDBCTools.connect(driver, url);
        var data:Fields[];
        if(cn==null)return data;
        var statement:Statement = cn.createStatement();
        var resultset:ResultSet;
        statement.setMaxRows(limit);
        statement.execute(sql);
        resultset=statement.getResultSet();
        if(resultset!=null){
            var meta:ResultSetMetaData=resultset.getMetaData();
            while (resultset.next()){
                var record:Fields=Fields{};
                for(i:Integer in [1 .. meta.getColumnCount()]){
                    record.set(meta.getColumnName(i), resultset.getString(meta.getColumnName(i)));
                    //var name:String=meta.getColumnName(i);
                    //record.node(name).asString("").value=resultset.getString(name);
                    //var field:NamedString=NamedString{key:name value:resultset.getString(name)};
                    //insert field into record.data;
                    //if(name.toUpperCase().equals(key.toUpperCase()))record.key=field.value;
                    }
                insert record into data;
                }
            resultset.close();
            }
        statement.close();
        return data;
        }
    }
    
